package com.innext.kjqb.ui.authentication_new.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.innext.kjqb.base.BaseActivity;
import com.innext.kjqb.events.AuthenticationCenterEvent;
import com.innext.kjqb.ui.authentication_new.bean.FirstVerificationInfoBean;
import com.innext.kjqb.ui.authentication_new.contract.AuthenticationCenterContract;
import com.innext.kjqb.ui.authentication_new.fragment.LinkmanFragment;
import com.innext.kjqb.ui.authentication_new.fragment.OperatorFragment;
import com.innext.kjqb.ui.authentication_new.fragment.PersonalInformationFragment;
import com.innext.kjqb.ui.authentication_new.fragment.SesameLetterFragment;
import com.innext.kjqb.ui.authentication_new.presenter.AuthenticationCenterPresenter;
import com.innext.kjqb.widget.DrawableCenterTextView;
import com.innext.kjqb.R;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by gyl on 2017/6/13 0013.
 * 认证步骤
 */

public class AuthenticationCenterActivity extends BaseActivity<AuthenticationCenterPresenter> implements AuthenticationCenterContract.View {
    @BindView(R.id.tv_personal_information)
    TextView mTvPersonalInformation;
    @BindView(R.id.tv_linkman)
    TextView mTvLinkman;
    @BindView(R.id.tv_operator)
    TextView mTvOperator;
    @BindView(R.id.tv_sesame_letter)
    TextView mTvSesameLetter;
    @BindView(R.id.frame_fragment)
    FrameLayout mFrameFragment;
    @BindView(R.id.tv_close)
    DrawableCenterTextView mTvClose;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    private PersonalInformationFragment personalInformationFragment = new PersonalInformationFragment();//个人
    private LinkmanFragment linkmanFragment = new LinkmanFragment();//联系人
    private OperatorFragment operatorFragment = new OperatorFragment();//运营商
    private SesameLetterFragment sesameLetterFragment = new SesameLetterFragment();//芝麻
    private FragmentManager mSupportFragmentManager;
    private String mPhoneInfoUrl;
    private String mRiskCarditInfoUrl;
    private int mAuthStep;
    private AuthenticationCenterPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_authentication_center;
    }

    @Override
    public void initPresenter() {
        presenter = new AuthenticationCenterPresenter();
        presenter.init(this);
        presenter.getAuthenticationCenter();
    }

    @Override
    public void loadData() {
    }


    @OnClick({R.id.tv_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_close:
                finish();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshPerfectInfo(AuthenticationCenterEvent event) {
        setVisibleFragment(4);
    }

    @Override
    public void AuthenticationCenterSccess(FirstVerificationInfoBean bean) {
        if (bean != null && bean.getItem() != null) {
            mAuthStep = bean.getItem().getAuthStep();
            mPhoneInfoUrl = bean.getItem().getPhoneInfoUrl();
            mRiskCarditInfoUrl = bean.getItem().getRiskCarditInfoUrl();
            setVisibleFragment(mAuthStep);

            Log.i("111", mAuthStep + "");
            Log.i("222", mPhoneInfoUrl);
            Log.i("333", mRiskCarditInfoUrl);
        }
    }

    private void setVisibleFragment(int authStep) {
        mSupportFragmentManager = getSupportFragmentManager();
        replaceFragment(personalInformationFragment);
        if (authStep == 1) {
            mTvTitle.setText("实名认证");
            replaceFragment(personalInformationFragment);
            mTvPersonalInformation.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_personal_information), null, null);
            mTvLinkman.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_linkman), null, null);
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_operator), null, null);
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_sesame_letter), null, null);
        } else if (authStep == 2) {
            mTvTitle.setText("联系人");
            replaceFragment(linkmanFragment);
            mTvPersonalInformation.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.personal_information), null, null);
            mTvLinkman.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_linkman), null, null);
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_operator), null, null);
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_sesame_letter), null, null);
        } else if (authStep == 3) {
            mTvTitle.setText("运营商");
            replaceFragment(operatorFragment);

            Bundle bundle = new Bundle();
            bundle.putString("PhoneInfoUrl", mPhoneInfoUrl);
            operatorFragment.setArguments(bundle);

            mTvPersonalInformation.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.personal_information), null, null);
            mTvLinkman.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.linkman), null, null);
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_operator), null, null);
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_sesame_letter), null, null);
        } else if (authStep == 4) {
            mTvTitle.setText("芝麻授信");
            replaceFragment(sesameLetterFragment);

            Bundle bundle = new Bundle();
            bundle.putString("RiskCarditInfoUrl", mRiskCarditInfoUrl);
            sesameLetterFragment.setArguments(bundle);

            mTvPersonalInformation.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.personal_information), null, null);
            mTvLinkman.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.linkman), null, null);
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.operator), null, null);
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_sesame_letter), null, null);
        }

        OnButtonClick();
    }

    private void OnButtonClick() {
        personalInformationFragment.setOnButtonClick(new PersonalInformationFragment.OnButtonClick() {
            @Override
            public void onClick(View view) {
                replaceFragment(linkmanFragment);
            }
        });

        linkmanFragment.setOnButtonClick(new LinkmanFragment.OnButtonClick() {
            @Override
            public void onClick(View view) {
                replaceFragment(operatorFragment);
            }
        });
    }

    private void replaceFragment(Fragment laterfragment) {
        mSupportFragmentManager.beginTransaction()
                .replace(R.id.frame_fragment, laterfragment)
                .commitAllowingStateLoss();
    }

    @Override
    public void showLoading(String content) {

    }

    @Override
    public void stopLoading() {

    }

    @Override
    public void showErrorMsg(String msg, String type) {

    }
}
